﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;

namespace FetchDataSystem.DAL
{
    public class UserService
    {
        /// <summary>
        /// 根据用户名查询用户信息
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <returns>是否存在</returns>
        public static bool CheckUser(string loginName)
        {
            int result = 0;
            string sql = "SELECT COUNT(*) FROM sys_user WHERE login_name=@loginName";
            try
            {
                result = Convert.ToInt32(DBHelper.ExecuteScalar(DBHelper.ConnectionString, CommandType.Text, sql, new MySqlParameter("@loginName", loginName)));
                if (result > 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return false;

        }

        /// <summary>
        /// 根据用户名密码查询用户信息，验证登录
        /// </summary>
        /// <param name="loginName">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>

        public static string CheckLogin(String loginName,String pwd)
        {
            string sql = "SELECT * from sys_user WHERE login_name=@loginName AND password=@pwd";
            try
            {
                using (MySqlDataReader dr = DBHelper.ExecuteReader(DBHelper.ConnectionString, CommandType.Text, sql, new MySqlParameter[]
        {
                new MySqlParameter("@loginName",loginName),
                new MySqlParameter("@pwd",pwd)
        }))
                {
                    if (dr.Read())
                    {
                        return dr["id"].ToString(); ;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            return null;

        }
    }
}
